package com.wgtgt.net_;

import java.util.Date;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;

/**
 * 断网检测
 * 
 */
public class App {
	public static void main(String[] args) {
		System.out.println("Hello World!");
		while (true) {
			String urlString = "http://127.0.0.1:8080/";
			String string = null;
			try {
				string = HttpUtil.get(urlString, 5000);
			} catch (Exception e) {
//				e.printStackTrace();
				if ("cn.hutool.core.io.IORuntimeException".equals(e.getClass().getName())) {
					if ("SocketTimeoutException: connect timed out".equals(e.getMessage())) {
						System.out.println("[断网]" + DateUtil.format(new Date(), "HH:mm:ss"));
						try {
							Thread.sleep(1000);
						} catch (InterruptedException e1) {

						}
						continue;
					} else {
						System.out.println("其他错误1");
					}
				} else {
					System.out.println("其他错误2");
				}
			}
			if (string.contains("Welcome to nginx!")) {
				System.out.println(DateUtil.format(new Date(), "HH:mm:ss"));
			} else {
				System.out.println("[断网]" + DateUtil.format(new Date(), "HH:mm:ss"));
			}
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
